#ifndef __BLEMASTER_H__
#define __BLEMASTER_H__

#include <stdio.h>
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#define MCU_ADV_HEAD1 0xF1
#define MCU_ADV_HEAD2 0x5F

#define MCU_BLE_HEAD1 0xF1
#define MCU_BLE_HEAD2 0x7F
#define ADV_MSG_BUF   1024

#define MAC_BYTE_SIZE     6     //MAC最大字节数
#define SSID_BYTE_SIZE    20    //ssid名字最大字节数




extern rt_mailbox_t mcuRxMail; //mcu收到蓝牙主机的
extern rt_mailbox_t mcuTxMail; //mcu发送给蓝牙主机的


typedef enum {
	ADV_CMD_REQPORTED_ACK  = 0x01,//上报数据
	ADV_CMD_SET_MODE_REQ   = 0X02,//设置模式请求
	ADV_CMD_SET_MODE_ACK   = 0x82,//设置模式响应
	BLE_CMD_BOUNDDEV_REQ   = 0x03,//绑定设备请求
	BLE_CMD_BOUNDDEV_ACK   = 0x83,//绑定设备响应
	BLE_CMD_DEVSLEEP_REQ   = 0x04,//设备休眠请求
	BLE_CMD_DEVSLEEP_ACK   = 0x84,//设备休眠响应
	BLE_CMD_DEVCAP_REQ     = 0x05,//从机测量数据请求
	BLE_CMD_DEVCAP_ACK     = 0x85,//从机测量数据结果
	BLE_CMD_STOPCAP_REQ    = 0x06,//从机停止数据请求
	BLE_CMD_STOPCAP_ACK    = 0X86,//从机停止数据请求应答
}bleCmdDef; //蓝牙主机端命令枚举;


typedef struct {
	unsigned char head[2]; //报文标记头
	unsigned char len;     //报文长度
	unsigned char cmd;     //命令码
	unsigned char mode;    //模式;0:添加扫描模式;1:正常扫描模式
	unsigned char mac[MAC_BYTE_SIZE];  //蓝牙从机MAC地址
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char x[2];    //陀螺仪X轴数据
	unsigned char y[2];   //陀螺仪Y轴数据
	unsigned char z[2];   //陀螺仪Z轴数据
	signed   char rssi;   //从机获取到最大RSSI值
	unsigned char rssiMac[MAC_BYTE_SIZE];
  unsigned char rssiSsid[SSID_BYTE_SIZE];
	unsigned char KEY[4]; //保留字段
	unsigned char crc16[2]; //CRC16校验码
}advReporotedDataDef; //蓝牙扫描主机上报数据结构体;

typedef struct {
	unsigned char head[2];   //报文标记头
	unsigned char len;//报文长度
	unsigned char cmd;//命令码
	unsigned char ID[2];//主机ID
	unsigned char mode;//模式;0:添加设备扫描模式;1:正常工作模式
	unsigned char crc16[2];
}advSetModeReqDef; //设置广播主机模式请求数据结构体;

typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;//报文长度
	unsigned char cmd;//命令码
	unsigned char result;//结果;0是成功;1是失败
	unsigned char crc16[2];//CRC16校验码
}advSetModeAckDef; //设置广播主机模式响应数据结构体;

typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;//报文长度
	unsigned char cmd;//命令码
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	unsigned char id[2];//主机ID
	unsigned char bound;//绑定状态;0:解绑;1:已绑定
	unsigned char crc16[2];//CRC16校验码
}bleBoundDevReqDef; //蓝牙主机绑定设备请求数据结构体;

typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;
	unsigned char cmd;//命令码
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	unsigned char result;//结果;0是成功;1是失败
	unsigned char crc16[2];//CRC16校验码
}bleBoundDevAckDef; //蓝牙主机绑定设备响应数据结构体;


typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;
	unsigned char cmd;//命令码
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	unsigned char sleep;//休眠状态;0:休眠
	unsigned char crc16[2];//CRC16校验码
}bleDevSleepReqDef; //蓝牙主机通知从设备休眠请求数据结构体;

typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;
	unsigned char cmd;//命令码
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	unsigned char result;//结果;0是成功;1是失败
	unsigned char crc16[2];//CRC16校验码
}bleDevSleepAckDef; //蓝牙主机通知从设备休眠响应数据结构体;


typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;
	unsigned char cmd;//命令码;
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	unsigned char crc16[2];//CRC16校验码
}bleDevCapReqDef; //设备陀螺仪请求数据结构体;

typedef struct {
	unsigned char head[2];//报文标记头;
	unsigned char len;
	unsigned char cmd;//命令码;
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	signed   char x[2];//陀螺仪X轴数据
	signed   char y[2];//陀螺仪Y轴数据
	signed   char z[2];//陀螺仪Z轴数据
	unsigned char rssi;//0 成功 1失败
  unsigned char rssiMac[MAC_BYTE_SIZE];
  unsigned char rssiSsid[SSID_BYTE_SIZE];
	unsigned char key[4]; //按键值
	unsigned char result;//结果
	unsigned char crc16[2];//CRC16校验码;
}bleDevCapAckDef; //设备陀螺仪响应数据结构体;


typedef struct {
	unsigned char head[2];//报文标记头
	unsigned char len;
	unsigned char cmd;//命令码;
	unsigned char ssid[SSID_BYTE_SIZE];//蓝牙从机SSID名称
	unsigned char mac[MAC_BYTE_SIZE];//蓝牙从机MAC地址
	unsigned char crc16[2];//CRC16校验码;
}bleDevStopCapReqDef; //蓝牙主机设备陀螺仪关闭请求数据结构体;


typedef struct {
	unsigned char head[2];
  unsigned char len;
	unsigned char cmd;
	unsigned char ssid[SSID_BYTE_SIZE];
	unsigned char mac[MAC_BYTE_SIZE];
	unsigned char result;
	unsigned char crc16[2];
}bleDevStopCapAckDef; //设备陀螺仪关闭响应数据结构体;


typedef union {
	//扫描主机
	advReporotedDataDef advReport;
	advSetModeReqDef    advSetModeReq;
	advSetModeAckDef    advSetModeAck;

	//连接主机
	bleBoundDevReqDef    boundDevReq;
	bleBoundDevAckDef    boudnDevAck;
	bleDevSleepReqDef    devSleepReq;
	bleDevSleepAckDef    devSleepAck;
	bleDevCapReqDef      devCapReq;
	bleDevCapAckDef      devCapAck;
	bleDevStopCapReqDef  devStopCapReq;
	bleDevStopCapAckDef  devStopCapAck;
}bleProtoUnionDef; //协议数据结构体;

typedef union {
	bleProtoUnionDef proto;
	unsigned char buf[sizeof(bleProtoUnionDef)];
}bleMsgDef; //接收数据结构体;

typedef struct
{
  bleMsgDef    msg;
  rt_uint8_t   msgLen;
  rt_uint32_t  outtime;//处理超时
  rt_uint8_t   cmd;
}bleMailDef; //ble主机邮箱定义


typedef struct
{
  rt_uint8_t  mac[MAC_BYTE_SIZE];//通讯的MAC
  rt_uint32_t outtime;           //操作超时10s
  rt_uint8_t  isBusy;            //1忙
  rt_uint8_t  cmd;               //收到0x04 会释放资源
}bleSelectDef;//蓝牙选择结构定义

void adv_master_init(void);


rt_err_t ble_recv_mail(bleMailDef *mail,rt_uint8_t *mac,bleCmdDef cmd,rt_size_t outimex2);//获取指定的蓝牙主机报文

rt_bool_t mac_byte_equality(rt_uint8_t *mac1,rt_uint8_t *mac2);

rt_err_t send_ble_mail(bleMailDef *mail);



#endif


